package com.java2007.hotel.controller;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.pojo.FoodTable;
import com.java2007.hotel.service.FoodTableService;
import com.java2007.hotel.service.impl.FoodTableServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 处理餐桌有关的请求
 * @author csl
 * @version V1.0
 * @date 2021/01/21
 **/
@WebServlet("/hotel/foodTable")
public class TableServlet extends BaseServlet{
    private FoodTableService foodTableService = (FoodTableService) BeanFactory.getBean(Constant.FOOD_TABLE_SERVICE_IMPL);

    /**
     *更新餐桌状态
     * @param req
     * @param resp
     */
    public String  update(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String isBook = req.getParameter("isBook");
        System.out.println(id);
        System.out.println(isBook);
        try {
            int i = foodTableService.updateFoodTableStatus(Byte.parseByte(isBook), Integer.parseInt(id));
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }
        return  "redirect:"+req.getContextPath()+"/hotel/foodTable?method=selectAll";
    }

    /**
     *删除餐桌
     * @param req
     * @param resp
     */
    public String  delete(HttpServletRequest req, HttpServletResponse resp) {
        String tempId = req.getParameter("id");
        Integer integer = Integer.valueOf(tempId);
        try {
            int i = foodTableService.deleteFoodTableById(integer);
            if (i == 2){
                return "back:" + "由于订单中含有该桌子且未结账，禁止删除！%"+"hotel/foodTable?method=selectAll";
            }
            if (i == 3){
                return "back:" + "该桌子被预定，禁止删除！%"+"hotel/foodTable?method=selectAll";
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }

        return  "redirect:"+req.getContextPath()+"/hotel/foodTable?method=selectAll";
    }

    /**
     * 增加一个餐桌
     * @param req
     * @param resp
     * @return
     */
    public String  add(HttpServletRequest req, HttpServletResponse resp) {
        String bName = req.getParameter("bName").trim();
        Admin admin = (Admin) req.getSession().getAttribute("admin");
        FoodTable foodTable = new FoodTable(null, bName, (byte) 0, null, new Date(), new Date(), admin.getId(), null);
        try {
            int i = foodTableService.addFoodTable(foodTable);
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }
        return "redirect:"+req.getContextPath()+"/hotel/foodTable?method=selectAll";
    }

    /**
     * 处理查询所有餐桌的请求
     * @param req
     * @param resp
     */
    public String selectAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        System.out.println(foodTableService);
        List<FoodTable> foodTables = null;
        try {
            foodTables = foodTableService.selectAllFoodTable();
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }
        session.setAttribute("foodTables",foodTables);
        return "redirect:"+req.getContextPath()+"/hotel/backend/detail/table/tableList.jsp";
    }

    /**
     * 模糊查询餐桌
     * @param req
     * @param resp
     * @return
     */
    public String search(HttpServletRequest req, HttpServletResponse resp){
        String keyword = req.getParameter("keyword").trim();
        if (!StringUtils.isNullOrEmpty(keyword)){
            HttpSession session = req.getSession();
            List<FoodTable> foodTables = null;
            try {
                foodTables = foodTableService.selectFoodTableByName(keyword);
            } catch (SQLException e) {
                e.printStackTrace();
                return "toIndex";
            }
            session.setAttribute("foodTables",foodTables);
            return "redirect:"+req.getContextPath()+"/hotel/backend/detail/table/tableList.jsp";
        }else {
            return  "redirect:"+req.getContextPath()+"/hotel/foodTable?method=selectAll";
        }

    }

    /**
     * 查询餐桌是否存在
     * @param req
     * @param resp
     * @return
     */
    public String isbName(HttpServletRequest req, HttpServletResponse resp){
        String keyword = req.getParameter("bName").trim();
        if (!StringUtils.isNullOrEmpty(keyword)){

            try {
                List<FoodTable> foodTables = foodTableService.selectFoodTableByNameAccurate(keyword);
                if (foodTables.size() != 0){
                    return "backText:"+"err";
                }else {
                    return "backText:"+"success";
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return "toIndex";
            }

        }else {
            return "backText:"+"wu";
        }

    }

}
